#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
using namespace std;

class Solution {
public:
    int findShortestSubArray(vector<int>& nums) {
        //寻找数组的度
        map<int,int> M;
        for(int i = 0;i<nums.size();++i){
            ++M[nums[i]];
        }
        int maxValue = 0;
        set<int> P;
        for(auto i = M.begin();i!=M.end();++i){
            if(i->second>maxValue){
                P.clear();
                maxValue = i->second; 
                P.insert(i->first);
            }
            if(i->second==maxValue){
                P.insert(i->first);
            }
        }
        int result = INT16_MAX;
        for(auto i = P.begin();i!=P.end();++i){
            auto left = find(nums.begin(),nums.end(),*i);
            for(int j = nums.size()-1;j>=0;j--){
                if(nums[j]==*i){
                    result = min(result,j-int(left-nums.begin())+1);
                    break;
                }
            }
        }
        
        return result;
    }
};

int main(){
    Solution S;
    vector<int> nums = {1,2,2,3,1};
    S.findShortestSubArray(nums);
}